﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Add_RoomCategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        try
        {
            sqlhelper.conn.Open();
            string Check = "select * from HM_RoomCategory where HM_RCategoryId='" + HM_RCategoryId.Text.Trim() + "'";
            SqlCommand cmd = new SqlCommand(Check, sqlhelper.conn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
            dr.Close();
        }
        catch
        {
            Response.Write("<script>alert('出现错误，请检查相应代码！')</script>");
        }
        finally
        {
            sqlhelper.conn.Close();
        }
    }

    protected void Add_Rooms_Click(object sender, EventArgs e)
    {
        try
        {
            sqlhelper.conn.Open();
            string add = "insert into HM_RoomCategory values(@HM_RCategoryId,@HM_RName,@HM_RArea,@HM_RBedNum,@HM_RPrice,@HM_RAirCondition,@HM_RTV)";
            SqlCommand MyAdd = new SqlCommand(add, sqlhelper.conn);
            MyAdd.Parameters.Add("@HM_RCategoryId", SqlDbType.Int).Value = HM_RCategoryId.Text.Trim();
            MyAdd.Parameters.Add("@HM_RName", SqlDbType.Char, 50).Value = HM_RName.Text.Trim();
            MyAdd.Parameters.Add("@HM_RArea", SqlDbType.Int).Value = HM_RArea.Text.Trim();
            MyAdd.Parameters.Add("@HM_RBedNum", SqlDbType.Int).Value = HM_RBedNum.Text.Trim();
            MyAdd.Parameters.Add("@HM_RPrice", SqlDbType.Money).Value = HM_RPrice.Text.Trim();
            MyAdd.Parameters.Add("@HM_RAirCondition", SqlDbType.Int).Value = HM_RAirConditionList.SelectedIndex;
            MyAdd.Parameters.Add("@HM_RTV", SqlDbType.Int).Value = HM_RTVList.SelectedIndex;
            MyAdd.ExecuteNonQuery();
            Response.Write("<script>alert('添加成功！')</script>");
            string add1 = "insert into HM_RoomStatus values(@HM_RCategoryId,1)";
            SqlCommand MyAdd1 = new SqlCommand(add1, sqlhelper.conn);
            MyAdd1.Parameters.Add("@HM_RCategoryId", SqlDbType.Int).Value = HM_RCategoryId.Text.Trim();
            MyAdd1.ExecuteNonQuery();
        }
        catch
        {
            Response.Write("<script>alert('添加失败，请查看相关代码！')</script>");
        }
        finally
        {
            sqlhelper.conn.Close();
        }
    }
}